package com.github.aloxc.plugin.restplus.component;

import com.github.aloxc.plugin.restplus.utils.StringUtils;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.IconLoader;
import com.intellij.ui.components.JBPanel;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * 快速搜索
 * @author leero
 */
public class QuickSearch extends JBPanel{

    public JPanel urlPanel;
    private JPanel rootPanel;
    private JTextField searchKey;
    private JButton searchButton;
    private JLabel filterText;
    private String url;

    public RequestEnvironmentListener getRequestEnvironmentListener() {
        return requestEnvironmentListener;
    }

    private RequestEnvironmentListener requestEnvironmentListener;


    public static QuickSearch getInstance(Project p) {
        return p.getComponent(QuickSearch.class);
    }


    private QuickSearch() {
        super();
        initComponent();
    }

    public void initComponent() {
        initUi();
        initActions();

    }

    private void initActions() {
        bindSearchActionListener();
        bindSearchKeyListener();

    }


    private class RequestEnvironmentListener extends  MouseAdapter implements ActionListener{
        @Override
        public void mouseClicked(MouseEvent e) {
            int height = 420;
            int width = 643;
            if(StringUtils.isNullOrEmpty(url)) {
                return;
            }
            RequestSetting setting = new RequestSetting(null,url,null);
            setting.setMaximumSize(new Dimension(width, height));
            setting.setMinimumSize(new Dimension(width, height));
            setting.setPreferredSize(new Dimension(width, height));
            Dialog dialog = new Dialog(null, setting,
                    new Dimension(width, height),
                    new Dimension(width, height),
                    new Dimension(width, height),
                    IconLoader.getIcon("/icons/setting.png"),
                    "Request variate(s)"
            );
            setting.setDialog(dialog);
            dialog.setVisible(true);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            this.mouseClicked(null);
        }
    }

//    private void bindEnvironmentListener() {
//        envSelectButton.addMouseListener(new MouseAdapter() {
//            @Override
//            public void mouseClicked(MouseEvent e) {
//                super.mouseClicked(e);
//                int x = e.getX();
//                int y = e.getY();
//                JPopupMenu menu = new JPopupMenu();        //创建菜单
//                for (int i = 0; i < 30; i++) {
//                    JMenuItem item = new JMenuItem("env " + i);//创建菜单项(点击菜单项相当于点击一个按钮)
//                    item.setIcon(IconLoader.getIcon("/icons/num/" + (i + 1) + ".png"));
//                    menu.add(item);
//                    item.addActionListener(new ActionListener() {
//                        @Override
//                        public void actionPerformed(ActionEvent e) {
//                            JMenuItem item = (JMenuItem) e.getSource();
//                            Messages.showMessageDialog("点击了环境切换按钮111" + item.getText(), "环境切换", IconLoader.getIcon("/icons/env.png"));
//                        }
//                    });
//                }
//                menu.show(envSelectButton, x, y);
//            }
//        });
//    }




    /**
     * 搜索输入框按键 监听器
     */
    private void bindSearchKeyListener() {
        this.searchKey.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
            }

            @Override
            public void keyPressed(KeyEvent e) {
            }

            @Override
            public void keyReleased(KeyEvent e) {
                //回车直接搜索
                if (e.getKeyCode() == 10) {
//                    try {
//                        if (searchKey != null && responseTextArea != null) {
//                            responseTextArea.search(searchKey);
//                        }
//                    } catch (BadLocationException var3) {
//                        var3.printStackTrace();
//                    }
                }
            }
        });
    }

    /**
     * 搜索按钮动作监听
     */
    private void bindSearchActionListener() {
        this.searchButton.addActionListener(event -> {
//            try {
//                if (searchKey != null && responseTextArea != null) {
//                    responseTextArea.search(searchKey);
//                }
//            } catch (BadLocationException e) {
//                e.printStackTrace();
//            }
        });
    }


    @Override
    protected void printComponent(Graphics g) {
        super.printComponent(g);
    }

    private void initUi() {
        GridLayoutManager rightLayoutManager = new GridLayoutManager(1, 3);
        rightLayoutManager.setHGap(1);
        rightLayoutManager.setVGap(1);

        initRequestPanel();

    }

    private void initRequestPanel() {
        searchKey.setAutoscrolls(true);
        urlPanel = new JBPanel();
        GridLayoutManager mgr = new GridLayoutManager(1, 3);
        mgr.setHGap(5);
        mgr.setVGap(1);
        mgr.setMargin(new Insets(0, 5, 0, 5));
        urlPanel.setLayout(mgr);
        urlPanel.add(filterText,
                new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_SOUTHEAST, GridConstraints.FILL_BOTH,
                        GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED,
                        null, null, null));
        urlPanel.add(searchKey,
                new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_SOUTHEAST, GridConstraints.FILL_BOTH,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                        null, null, null));
        urlPanel.add(searchButton,
                new GridConstraints(0, 2, 1, 1, GridConstraints.ANCHOR_SOUTHEAST, GridConstraints.FILL_BOTH,
                        GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED,
                        null, null, null));
        this.setBorder(BorderFactory.createEmptyBorder());
        this.setLayout(new GridLayoutManager(2, 1));

        this.add(urlPanel,
                new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED,
                        null, null, null));
    }




}